package com.example.demo.controller;


import com.example.demo.common.AjaxResult;
import com.example.demo.common.Constant;
import com.example.demo.common.SessionUtil;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /*public List<ArticleInfo> mylist(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if(session!=null&&session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null){
            UserInfo userInfo=(UserInfo)session.getAttribute(Constant.SESSION_USERINFO_KEY);
            return articleService.getMyList(userInfo.getId());
        }
        return  null;
    }*/
    @RequestMapping("/mylist")
    public List<ArticleInfo> myList(HttpServletRequest request) {
        UserInfo userInfo = SessionUtil.getLoginUser(request);
        if (userInfo != null) {
            return articleService.getMyList(userInfo.getId());
        }
        return null;
    }

    @RequestMapping("/detail")
    public  Object getDetail(Integer aid){
        if(aid!=null&&aid>0){
            return  AjaxResult.success(articleService.getDetail(aid));
        }
        return AjaxResult.fail(-1,"查询失败");
    }
    @RequestMapping("/detailbyid")
    public  Object getDetailById(HttpServletRequest request,Integer aid){
        if(aid!=null&&aid>0){
            ArticleInfo articleInfo =articleService.getDetail(aid);
            UserInfo userInfo=SessionUtil.getLoginUser(request);
            if(userInfo!=null&&articleInfo!=null&&userInfo.getId()==articleInfo.getUid()){
                return  AjaxResult.success(articleInfo);
            }
        }
        return AjaxResult.fail(-1,"查询失败");
    }

    @RequestMapping("/update")
    public  int update(Integer aid,String title,String content,HttpServletRequest request){
        if(!StringUtils.hasLength(content)||!StringUtils.hasLength(title)){
            return  0;
        }
        UserInfo userInfo =SessionUtil.getLoginUser(request);
        if(userInfo!=null&&userInfo.getId()>0){
           return articleService.update(aid,title,content,userInfo.getId()); //sql语句大部分默认返回受影响的行数
        }
        return  0;
    }
    @RequestMapping("/del")
    public  int del(Integer id){
        if(id!=null&&id>0){
            return articleService.del(id);
        }
        return  0;
    }
    @RequestMapping("/list")
    public  List<ArticleInfo> getList2(Integer pindex,Integer psize){
        if(psize==null||pindex==null){
            return null;
        }
        int offset=(pindex-1)*psize;
        return  articleService.getList2(psize,offset);
    }

    @RequestMapping("/gettotalpage")
    public  Integer gettotalpage(Integer psize){
        if(psize!=null){
            int total=articleService.gettotalpage();
            int totalpage=(int) Math.ceil(total*1.0/psize); //ceil是小数点进一,但为double类型
            return  totalpage;
        }
        return null;
    }

    @RequestMapping("/gett")
    public  Integer gett(Integer uid){
        if(uid!=null){
            int total=articleService.gett(uid);
            return  total;
        }
        return null;
    }

    @RequestMapping("/writeblog")
    public int writeblog(HttpServletRequest request,String title,String content){
        if(!StringUtils.hasLength(content)||!StringUtils.hasLength(title)){
            return  -1;
        }
        UserInfo userInfo =SessionUtil.getLoginUser(request);
        if(userInfo!=null&&userInfo.getId()>0){
            return articleService.writeblog(userInfo.getId(),title,content); //sql语句大部分默认返回受影响的行数
        }
        return  -1;
    }
}
